package com.xjrsoft.module.ureport.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.core.tool.utils.StringPool;
import com.xjrsoft.core.tool.utils.StringUtil;
import com.xjrsoft.module.base.entity.XjrBaseModule;
import com.xjrsoft.module.base.service.IXjrBaseModuleService;
import com.xjrsoft.module.ureport.dto.GetRelationPageListDto;
import com.xjrsoft.module.ureport.dto.RptRelationDto;
import com.xjrsoft.module.ureport.entity.XjrReportRelease;
import com.xjrsoft.module.ureport.entity.XjrRptRelation;
import com.xjrsoft.module.ureport.mapper.XjrRptRelationMapper;
import com.xjrsoft.module.ureport.service.IXjrRptRelationService;
import com.xjrsoft.module.ureport.vo.RptRelationVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 报表菜单关联关系表 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2020-12-10
 */
@Service
public class XjrRptRelationServiceImpl extends ServiceImpl<XjrRptRelationMapper, XjrRptRelation> implements IXjrRptRelationService {
    @Autowired
    private IXjrBaseModuleService moduleService;

    @Override
    public PageOutput<RptRelationVo> getPageList(GetRelationPageListDto dto) {
        String keyword = dto.getKeyword();
        if (!StringUtil.isEmpty(keyword)) {
            keyword = "%" + keyword + "%";
        }
        IPage<RptRelationVo> page = ConventPage.getPage(dto);
        List<RptRelationVo> resultList = baseMapper.getPageList(keyword, page);
        return ConventPage.getPageOutput(page.getTotal(), resultList);
    }

    @Override
    public boolean saveRptRelation(RptRelationDto dto) throws Exception {
        //判断父级菜单是否为功能菜单
        String parentId = dto.getParentId();
        XjrBaseModule module = moduleService.getById(parentId);
        if(module!=null&&"iframe".equals(module.getTarget())){
            throw new Exception("父级菜单不能为功能菜单!!!");
        }
        String idStr = IdWorker.getIdStr();
        XjrRptRelation xjrRptRelation = BeanUtil.copy(dto, XjrRptRelation.class);
        xjrRptRelation.setId(StringUtil.randomUUID());
        xjrRptRelation.setModuleId(idStr);
        XjrBaseModule savedModule = BeanUtil.copy(dto, XjrBaseModule.class);
        savedModule.setModuleId(idStr);
        String rptFileId = xjrRptRelation.getRptFileId();
        savedModule.setUrlAddress(StringPool.SLASH + rptFileId.substring(0, rptFileId.indexOf(".")));
        savedModule.setComponent("views/ReportModule/ReportFrame");
        savedModule.setTarget("iframe");
        savedModule.setIsMenu(1);
        return baseMapper.insert(xjrRptRelation) > 0 && moduleService.save(savedModule);
    }

    @Override
    public boolean updateRptRelation(String id, RptRelationDto dto) throws Exception {
        //判断父级菜单是否为功能菜单
        String parentId = dto.getParentId();
        XjrBaseModule module = moduleService.getById(parentId);
        if(module != null && "iframe".equals(module.getTarget())){
            throw new Exception("父级菜单不能为功能菜单!!!");
        }
        XjrRptRelation xjrRptRelation = BeanUtil.copy(dto, XjrRptRelation.class);
        xjrRptRelation.setId(id);
        XjrBaseModule savedModule = BeanUtil.copy(dto, XjrBaseModule.class);
        return this.updateById(xjrRptRelation) && moduleService.updateById(savedModule);
    }

    @Override
    public boolean deleteRptRelation(String ids) {
        boolean flag=false;
        List<String> idList = Arrays.asList(ids.split(StringPool.COMMA));
        List<XjrRptRelation> xjrRptRelationList = this.listByIds(idList);
        if (xjrRptRelationList != null) {
            List<String> moduleIds = xjrRptRelationList.stream().map(XjrRptRelation::getModuleId).collect(Collectors.toList());
            flag=this.removeByIds(idList);
            moduleService.removeByIds(moduleIds);
        }
        return flag;
    }
}
